package com.xbxkj.mix.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.xbxkj.mix.controller.BaseController;
import com.xbxkj.mix.util.AuthSign;
import com.xbxkj.mix.util.result.ApiCode;
import com.xbxkj.mix.util.result.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Zhang on 2018/8/26.
 */
@Configuration
@Slf4j
public class ServertInterceptor extends BaseController implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("请求已进入拦截器",getIpAddr());
        String token = request.getHeader("Authorization");
        if(AuthSign.verify(token)){
            return true;
        }else{
            renderJson(JSONObject.toJSONString(ApiResult.fail(ApiCode.LOGIN_OUT )));
            log.error("请求地址为：{}的Token校验失败",request.getRequestURL());
            return false;
        }


    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }


}
